package com.aaronprj.school.mbooks.resource;

import java.util.List;
import java.util.Map.Entry;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import com.aaronprj.common.web.resource.ResourceServices;
import com.aaronprj.school.mbooks.uivo.UINonJAXBBean;
import com.aaronprj.school.mbooks.uivo.UITestObj;

@Path("/test")
public class TestResource extends ResourceServices{

	@Context UriInfo uriInfo; 
	//Context HttpServletRequest request;
	
	@Path("/param")
    @GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    public String testGetMethod() {
		
		UITestObj t = new UITestObj();
		t.setId(111);
		t.setName("my test get");
		t.setPrice(12.33);
		t.setDiscription("show me the power 0000");
		
		System.out.println(t.toString());
		
		System.out.println(getWebApplicationRealPath("pix"));

    	return this.writeValueAsString(t);
	}
	
	@Path("/param")
    @POST
	@Produces(MediaType.APPLICATION_JSON)
    public String testPOSTMethod() {
		UITestObj testobj = new UITestObj();
		MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
		//MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
		
		for (Entry<String, List<String>> cfg : queryParams.entrySet()) {
			setStateField(testobj, cfg.getKey(), cfg.getValue().get(0));
		}
		
		System.out.println(testobj.toString());
		

    	return this.writeValueAsString(testobj);
	}

	@Path("/nonjaxb")
    @GET
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    public String testGetNonJAXBMethod() {
		
		UINonJAXBBean t = new UINonJAXBBean();
		t.id = 111;
		t.name = "nonjaxb, my test get";
		t.price = 12.11;
		t.discription = "nonjaxb, show me the power 1111";
		
		System.out.println(t.toString());
		

    	return this.writeValueAsString(t);
	}
	
	@Path("/nonjaxb")
    @POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
    public String testPOSTNonJAXBMethod(UINonJAXBBean testobj) {

		System.out.println(testobj.toString());
		

    	return this.writeValueAsString(testobj);
	}

}
